﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;
using YJKFloor.CreateFloor;
using YJKFloor.EnumDefine;
using YJKFloor.FloorEditor;

namespace YJKFloor.Forms
{
	public partial class SearchFloorForm : System.Windows.Forms.Form
	{
		public int LevelIndex
		{
			get
			{
				return this.cmbLevel.SelectedIndex;
			}
		}

		public FloorType SelFloorType
		{
			get
			{
				return this.m_FloorTypeList[this.cmbFloorType.SelectedIndex];
			}
		}

		public double ElevationOffset
		{
			get
			{
				double result = 0.0;
				try
				{
					result = Convert.ToDouble(this.cmbOffset.Text);
				}
				catch (Exception)
				{
					this.cmbOffset.Text = "0";
					return 0.0;
				}
				return result;
			}
		}

		public double BoundaryOffset
		{
			get
			{
				double result = 0.0;
				try
				{
					result = Convert.ToDouble(this.tbExtend.Text);
				}
				catch (Exception)
				{
					return this.m_Extend;
				}
				return result;
			}
		}

		public FilterType TheFilterType
		{
			get
			{
				FilterType filterType = FilterType.FT_None;
				if (this.chkBeam.Checked)
				{
					filterType |= FilterType.FT_Beam;
				}
				if (this.chkWall.Checked)
				{
					filterType |= FilterType.FT_Wall;
				}
				if (this.chkGrid.Checked)
				{
					filterType |= FilterType.FT_Grid;
				}
				if (this.chkRefPlane.Checked)
				{
					filterType |= FilterType.FT_RefPlane;
				}
				if (this.chkColumn.Checked)
				{
					filterType |= FilterType.FT_Column;
				}
				return filterType;
			}
		}

		public bool IsOutloop
		{
			get
			{
				return this.rbOutloop.Checked;
			}
		}

		public bool IsPickBox
		{
			get
			{
				return this.rbPickBox.Checked;
			}
		}

		public bool IsClosed
		{
			get
			{
				return this.m_Closed;
			}
		}

		public bool SelectTypeChange
		{
			get
			{
				return this.m_SelectTypeChange;
			}
			set
			{
				this.m_SelectTypeChange = value;
			}
		}

		public bool IsClickMode
		{
			get
			{
				return this.m_bIsClickMode;
			}
			set
			{
				this.m_bIsClickMode = value;
			}
		}

		public SelectType SelType
		{
			get
			{
				return this.m_SelectType;
			}
			set
			{
				this.m_SelectType = value;
			}
		}

		public SearchFloorForm(ExternalCommandData cmdData, List<FloorType> floorTypeList, List<string> levelNameList, string currentLevelName)
		{
			this.m_Revit = cmdData;
			this.m_Doc = this.m_Revit.Application.ActiveUIDocument.Document;
			this.m_FloorTypeList = floorTypeList;
			this.m_LevelNameList = levelNameList;
			this.m_CurrentLevelName = currentLevelName;
			this.InitializeComponent();
			this.radSelectRoom.Checked = true;
		}

		private void SearchFloorForm_Load(object sender, EventArgs e)
		{
			this.UpdateFloorTypeList(0);
			this.UpdateLevelList();
			this.ReadData();
		}

		private void SearchFloorForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
			this.m_Closed = true;
		}

		private void SearchFloorForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private void SearchFloorForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void rbOutloop_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateExtend();
		}

		private void UpdateExtend()
		{
			if (this.rbOutloop.Checked)
			{
				this.chkBeam.Enabled = false;
				this.chkColumn.Enabled = false;
				this.chkGrid.Enabled = false;
				this.chkRefPlane.Enabled = false;
				this.chkWall.Enabled = false;
				this.tbExtend.Enabled = true;
				this.tbExtend.Text = this.m_Extend.ToString("f0");
				return;
			}
			this.chkBeam.Enabled = true;
			this.chkColumn.Enabled = true;
			this.chkGrid.Enabled = true;
			this.chkRefPlane.Enabled = true;
			this.chkWall.Enabled = true;
			this.tbExtend.Text = "0";
			this.tbExtend.Enabled = false;
		}

		private void rbPickPoint_CheckedChanged(object sender, EventArgs e)
		{
			this.m_SelectTypeChange = true;
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void UpdateFloorTypeList(int index)
		{
			this.cmbFloorType.Items.Clear();
			foreach (FloorType floorType in this.m_FloorTypeList)
			{
				this.cmbFloorType.Items.Add(floorType.Name);
			}
			this.cmbFloorType.SelectedIndex = index;
		}

		private void UpdateLevelList()
		{
			this.cmbLevel.Items.Clear();
			int selectedIndex = 0;
			int num = 0;
			foreach (string text in this.m_LevelNameList)
			{
				if (text.CompareTo(this.m_CurrentLevelName) == 0)
				{
					selectedIndex = num;
				}
				this.cmbLevel.Items.Add(text);
				num++;
			}
			this.cmbLevel.SelectedIndex = selectedIndex;
		}

		private void ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SearchFloorForm.dat";
			if (!File.Exists(path))
			{
				this.FirstReadData();
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				Convert.ToInt32(streamReader.ReadLine());
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				this.chkBeam.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.chkWall.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.chkGrid.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.chkRefPlane.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.chkColumn.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbOutloop.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbCells.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbPickPoint.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbPickBox.Checked = Convert.ToBoolean(streamReader.ReadLine());
				int num = Convert.ToInt32(streamReader.ReadLine());
				for (int i = 0; i < num; i++)
				{
					this.cmbOffset.Items.Add(streamReader.ReadLine());
				}
				this.cmbOffset.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
				this.m_Extend = Convert.ToDouble(streamReader.ReadLine());
				this.UpdateExtend();
				int num2 = Convert.ToInt32(streamReader.ReadLine());
				if (num2 < this.cmbFloorType.Items.Count)
				{
					this.cmbFloorType.SelectedIndex = num2;
				}
				Convert.ToInt32(streamReader.ReadLine());
				for (int i = 0; i < this.cmbLevel.Items.Count; i++)
				{
					if (this.cmbLevel.Items[i].ToString().CompareTo(this.m_Doc.ActiveView.GenLevel.Name) == 0)
					{
						this.cmbLevel.SelectedIndex = i;
						break;
					}
				}
				streamReader.Close();
				fileStream.Close();
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				this.FirstReadData();
			}
		}

		private void FirstReadData()
		{
			base.StartPosition = FormStartPosition.CenterScreen;
			this.chkBeam.Checked = true;
			this.chkWall.Checked = true;
			this.chkGrid.Checked = true;
			this.chkRefPlane.Checked = true;
			this.chkColumn.Checked = true;
			this.rbOutloop.Checked = true;
			this.rbPickBox.Checked = true;
			this.cmbOffset.Items.Add(250);
			this.cmbOffset.Items.Add(200);
			this.cmbOffset.Items.Add(150);
			this.cmbOffset.Items.Add(100);
			this.cmbOffset.Items.Add(50);
			this.cmbOffset.Items.Add(0);
			this.cmbOffset.Items.Add(-50);
			this.cmbOffset.Items.Add(-100);
			this.cmbOffset.Items.Add(-150);
			this.cmbOffset.Items.Add(-200);
			this.cmbOffset.Items.Add(-250);
			this.cmbOffset.SelectedIndex = 5;
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SearchFloorForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_Version);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.chkBeam.Checked);
			streamWriter.WriteLine(this.chkWall.Checked);
			streamWriter.WriteLine(this.chkGrid.Checked);
			streamWriter.WriteLine(this.chkRefPlane.Checked);
			streamWriter.WriteLine(this.chkColumn.Checked);
			streamWriter.WriteLine(this.rbOutloop.Checked);
			streamWriter.WriteLine(this.rbCells.Checked);
			streamWriter.WriteLine(this.rbPickPoint.Checked);
			streamWriter.WriteLine(this.rbPickBox.Checked);
			if (Common.IsValidating(this.cmbOffset.Text, -500.0, 500.0))
			{
				bool flag = false;
				for (int i = 0; i < this.cmbOffset.Items.Count; i++)
				{
					if (this.cmbOffset.Text.CompareTo(this.cmbOffset.Items[i].ToString()) == 0)
					{
						flag = true;
						this.cmbOffset.SelectedIndex = i;
						break;
					}
				}
				if (!flag)
				{
					this.cmbOffset.Items.Add(this.cmbOffset.Text);
					this.cmbOffset.SelectedIndex = this.cmbOffset.Items.Count - 1;
				}
			}
			streamWriter.WriteLine(this.cmbOffset.Items.Count);
			foreach (object obj in this.cmbOffset.Items)
			{
				streamWriter.WriteLine(obj.ToString());
			}
			streamWriter.WriteLine(this.cmbOffset.SelectedIndex);
			streamWriter.WriteLine(this.m_Extend);
			streamWriter.WriteLine(this.cmbFloorType.SelectedIndex);
			streamWriter.WriteLine(this.cmbLevel.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			SearchFloorForm.SendMessage(CreateFloorCmd._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SearchFloorForm.keybd_event(bVk, 0, dwFlags, 0);
			SearchFloorForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void cmbOffset_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbOffset.Text.CompareTo("-") == 0)
			{
				return;
			}
			if (!Common.IsValidating(this.cmbOffset.Text, -500.0, 500.0))
			{
				MessageBox.Show("合法范围在-500mm～500mm之间", "盈建科提示");
				this.cmbOffset.Text = this.cmbOffset.Items[5].ToString();
			}
		}

		private void tbExtend_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbExtend.Text, 0.0, 1500.0))
			{
				MessageBox.Show("合法范围在0mm～1500mm之间", "盈建科提示");
				this.tbExtend.Text = this.m_Extend.ToString("f0");
				return;
			}
			this.m_Extend = Convert.ToDouble(this.tbExtend.Text);
		}

		private void btnNewFloorType_Click(object sender, EventArgs e)
		{
			FloorType selFloorType = this.SelFloorType;
			FloorThicknessEditer floorThicknessEditer = new FloorThicknessEditer(this.m_Revit);
			Transaction transaction = new Transaction(this.m_Doc);
			transaction.Start("new floor type");
			FloorType floorType = null;
			string text = "";
			if (!floorThicknessEditer.NewFloorType(selFloorType, ref floorType, ref text))
			{
				transaction.RollBack();
				return;
			}
			transaction.Commit();
			this.m_FloorTypeList.Add(floorType);
			this.cmbFloorType.Items.Add(floorType.Name);
			this.cmbFloorType.Text = floorType.Name;
		}

		private void radManualDraw_Click(object sender, EventArgs e)
		{
			this.m_SelectType = SelectType.ST_FreeDraw;
			this.m_bIsClickMode = true;
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void radSelectRoom_Click(object sender, EventArgs e)
		{
			this.m_SelectType = SelectType.ST_SelectRoom;
			this.m_bIsClickMode = true;
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void radMutilSelectRoom_Click(object sender, EventArgs e)
		{
			this.m_SelectType = SelectType.ST_MutilSelectRoom;
			this.m_bIsClickMode = true;
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;

		private List<FloorType> m_FloorTypeList;

		private List<string> m_LevelNameList;

		private string m_CurrentLevelName = "";

		private bool m_Closed;

		private bool m_SelectTypeChange;

		private bool m_bIsClickMode;

		private SelectType m_SelectType = SelectType.ST_SelectRoom;

		private double m_Extend;

		private int m_Version = 1;
	}
}
